<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="1500px" >
			<TableSearch :search="state.tableData.search" @search="onSearch" />
			<Table
				ref="tableRef"
				v-bind="state.tableData"
				class="table-admin"
				@updateRow="onTableUpdateRow"
				@add="onTableAddRow"
				@delRow="onTableDelRow"
				@pageChange="onTablePageChange"
				@sortHeader="onSortHeader"
			/>
			<EditDialog  ref="editRef"  @refresh="getTableData()" />
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">关闭</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="cgdictdataLists">
import { defineAsyncComponent, reactive, ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { getList,getListAll,del,change } from '/@/api/cgdictdata/index';
import { getListsIdName as getcgdicttypedict_nameListsIdName } from '/@/api/cgdicttype/index';

import { handleTree } from '/@/utils';

// 引入组件
const Table = defineAsyncComponent(() => import('/@/components/table/index.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/components/table/search.vue'));
const EditDialog = defineAsyncComponent(() => import('/@/views/cogen/cgdictdata/form/edit.vue'));

// 定义变量内容
const tableRef = ref<RefType>();
const editRef = ref();
const state = reactive<TableDemoState>({
		tableData: {
			// 列表数据（必传）
			data: [],
			// 表头内容（必传，注意格式）
			header: [
			{ key: 'id', colWidth: '100', title: '字典编码', type: '', isCheck: true },
			{ key: 'dict_label', colWidth: '', title: '字典标签', type: 'text', isCheck: true },
			{ key: 'dict_value', colWidth: '', title: '字典键值', type: 'text', isCheck: true },
			{ key: 'is_default', colWidth: '100', title: '是否默认', type: 'radio',dict:{"0":"否","1":"是"}, isCheck: true },
			{ key: 'status', colWidth: '100', title: '状态', type: 'radio',dict:{"0":"停用","1":"正常"}, isCheck: true },
			{ key: 'dict_sort', colWidth: '100', title: '字典排序', type: 'text', isCheck: true },
			{ key: 'remark', colWidth: '100', title: '备注', type: 'textarea', isCheck: true },
		],
		dialog: {
			isShowDialog: false,
			type: '',
			title: '',
			submitTxt: '',
		},
		// 配置项（必传）
		config: {
			total: 0, // 列表总数
			loading: true, // loading 加载
			isButton: true,
			isBorder: false, // 是否显示表格边框
			isSerialNo: false, // 是否显示表格序号
			isSelection: true, // 是否显示表格多选
			isOperate: true, // 是否显示表格操作栏
			isAdd: 'admin:cogencgdictdata:save', //新增标识
			isUpdate: 'admin:cogencgdictdata:save', //修改标识
			isDelete: 'admin:cogencgdictdata:del', //删除标识 
		},
		// 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
		search: [
        { label: '字典标签', prop: 'dict_label', placeholder: '请输入字典标签', required: false, type: 'input' },
    ],
		// 搜索参数（不用传，用于分页、搜索时传给后台的值，`getTableData` 中使用）
		param:  {
            	page: 1,
            	psize: 10,
            },
		//显示分页
		showpage:1,
		// 打印标题
		printName: '数据打印',
		//列字典
		dictData:{
        cg_dict_typedict_name:[],
    },
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	
});
// 打开弹窗
const openDialog = (type: string, row?: any) => {
	state.dialog.title = '字典值';
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	// 查询
	state.tableData.param = Object.assign(state.tableData.param,{'dict_type': row.dict_type });
	getTableData();
};
// 取消
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 初始化列表数据
const getTableData =  () => {
	state.tableData.config.loading = true;
	state.tableData.data = [];
	getList(state.tableData.param).then((response: any) => {
		state.tableData.data = response.data.data;
		state.tableData.config.total = response.data.total;
		state.tableData.config.loading = false;
	});
};

// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { ...data });
	tableRef.value.pageReset();
};
// 删除当前项回调
const onTableDelRow = (row: EmptyObjectType) => {
	return del(row.id).then(() => {
		getTableData();
		ElMessage.success(`删除${row.id}成功！`);
	});
};
// 分页改变时回调
const onTablePageChange = (page: TableDemoPageType) => {
	state.tableData.param.page = page.page;
	state.tableData.param.psize = page.psize;
	getTableData();
};
// 拖动显示列排序回调
const onSortHeader = (data: TableHeaderType[]) => {
	state.tableData.header = data;
};

/** 新增按钮操作 */
const onTableAddRow = () => {
	const row = Object.assign({'dict_type': state.tableData.param.dict_type})
	console.log(row)
	editRef.value.openDialog('add',row);
};
/** 修改按钮操作 */
const onTableUpdateRow = (row: any) => {
	editRef.value.openDialog('edit',row);
};


// 页面加载时
onMounted(() => {
	getTableData();
	// 关联查询
	    getcgdicttypedict_nameListsIdName({field:'dict_name',idkey:'dict_type'}).then((response: any) => {
                            state.tableData.dictData.cg_dict_typedict_name = response.data.data;
                        });

});
// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.table-admin-container {
	.table-admin-padding {
		padding: 15px;
		.table-admin {
			flex: 1;
			overflow: hidden;
		}
	}
}
</style>
